package com.loooooo.pm2024.web.session;

import com.loooooo.pm2024.MyApplication;
import com.loooooo.pm2024.config.RedisUtils;
import com.loooooo.cas.web.session.MyHttpSession;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.util.UUID;

public class SessionUtils {


    static RedisUtils redisUtils = MyApplication.getBean(RedisUtils.class);

    public static HttpSession getSession(boolean newIf) {
        SessionContext context = SessionContextHolder.get();
        MyServletRequestWrapper request = context.getRequest();
        MyHttpSession session = (MyHttpSession) request.getSession();
        if (session == null) {
            String cookieValue = getCookieValue(MyHttpSession.getCookieName(), request.getCookies());
            if (cookieValue != null) {
                session = (MyHttpSession) redisUtils.getObject("session:" + cookieValue);
                //myHttpSession = sessionMap.get(cookieValue);
            }
            if (session == null && newIf) {
                // 自定义生成一个唯一id
                String id = UUID.randomUUID().toString();
                // 生成了id需要添加cookie
                HttpServletResponse response = context.getResponse();
                Cookie cookie = new Cookie(MyHttpSession.getCookieName(), id);
                cookie.setPath("/");
                cookie.setHttpOnly(true);
                //cookie.setDomain("loooooo.com");
                cookie.setSecure(false);
                response.addCookie(cookie);
                session = new MyHttpSession(id);
            }
        }
        //MyServletRequestWrapper myServletRequestWrapper = new MyServletRequestWrapper(request);
        //myServletRequestWrapper.setSession(session);
        request.setSession(session);
        return session;
    }

    public static HttpSession getCASSession() {
        SessionContext context = SessionContextHolder.get();
        MyServletRequestWrapper request = context.getRequest();
        String cookieValue = getCookieValue("JHCAS", request.getCookies());
        if (cookieValue == null) {
            return null;
        }
        HttpSession session = (MyHttpSession) redisUtils.getAbsObject("cas:session:" + cookieValue);


        return session;
    }

    private static String getCookieValue(String name, Cookie[] cookies) {
        if (cookies == null)
            return null;
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
        return null;
    }

    public static void saveSession() {
        SessionContext context = SessionContextHolder.get();
        HttpServletRequest request = context.getRequest();
        MyHttpSession session = (MyHttpSession) request.getSession();
        redisUtils.setWithExpire("session:" + session.getId(), session, Duration.ofMinutes(60));
    }
}
